package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.FormDesignConstant;
import com.aplink.generic.pojo.Value;
import com.aplink.generic.pojo.ValueList;
import com.aplink.generic.pojo.controlspec.spinner.SpinnerSpec;
import com.aplink.generic.xmlparser.XmlPaserUtil;

public class SpinnerFormat {
	public static final String CONTENT_DATA = "contentdata";

	public static final String DISPLAYPROP = "displayprop";
	public static final String NEW_ROW_ITEM = "newRowItem";
	public static final String OBJ_LOAD_BY_CONFIG = "objloadbyconfig";
	public static final String REFRESH_CONTROL = "refreshControl";
	public static final String REFRESH_VALUE = "refreshValue";
	public static final String ROWITEM = "rowitem";
	public static final String RPTRDESIGN = "rptrdesign";
	public static final String VALUE_LIST = "valuelist";
	public static final String VALUEPROP = "valueprop";
	public static SpinnerSpec parse(final Node node) {
		final SpinnerSpec spinnerSpec = new SpinnerSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node,
					SpinnerFormat.REFRESH_CONTROL);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setRefreshControl(value);
			}

			value = XmlPaserUtil
					.getAttrValue(node, SpinnerFormat.REFRESH_VALUE);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setRefreshValue(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SpinnerFormat.VALUEPROP);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setValueProp(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SpinnerFormat.DISPLAYPROP);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setDisplayProperty(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SpinnerFormat.ROWITEM);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setRowItem(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SpinnerFormat.RPTRDESIGN);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setRptrdesign(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SpinnerFormat.CONTENT_DATA);
			if (!TextUtils.isEmpty(value)) {
				spinnerSpec.setContentData(value);
			}

			value = XmlPaserUtil.getAttrValue(node,
					SpinnerFormat.OBJ_LOAD_BY_CONFIG);
			if (!TextUtils.isEmpty(value)) {
				if (value.equalsIgnoreCase("N")) {
					spinnerSpec.setLoadByConfig(false);
				} else {
					spinnerSpec.setLoadByConfig(true);
				}
			} else {
				spinnerSpec.setLoadByConfig(true);
			}
		}
		final Element element = (Element) node;
		final NodeList nodeList = element
				.getElementsByTagName(SpinnerFormat.VALUE_LIST);
		if (nodeList.getLength() == 1) {
			spinnerSpec.setValueList(SpinnerFormat.parseValueList(nodeList
					.item(0)));
		}
		return spinnerSpec;
	}

	private static ValueList parseValueList(final Node nodeValueList) {
		final Element element = (Element) nodeValueList;
		final ValueList valueList = new ValueList();
		if (nodeValueList.hasAttributes()) {
			final String dataObj = XmlPaserUtil.getAttrValue(nodeValueList,
					FormDesignConstant.DATAOBJ);
			if (dataObj != null) {
				valueList.setDataObj(dataObj);
			}

			final String submitControl = XmlPaserUtil.getAttrValue(
					nodeValueList, FormDesignConstant.SUBMIT_CONTROL);

			if (submitControl != null) {
				valueList.setSubmitControl(submitControl);
			}

			final String valueControl = XmlPaserUtil.getAttrValue(
					nodeValueList, FormDesignConstant.VALUE_CONTROL);

			if (valueControl != null) {
				valueList.setValueControl(valueControl);
			}

			final String valueColumn = XmlPaserUtil.getAttrValue(nodeValueList,
					FormDesignConstant.VALUE_COLUMN);
			if (valueColumn != null) {
				valueList.setValueColumn(valueColumn);
			}
		}
		final NodeList nodeList = element
				.getElementsByTagName(FormDesignConstant.VALUE);
		for (int i = 0; i < nodeList.getLength(); i++) {
			final Value value = new Value();
			final Node valueItem = nodeList.item(i);
			if (valueItem.hasAttributes()) {
				final String mappingName = XmlPaserUtil.getAttrValue(valueItem,
						FormDesignConstant.MAPPINGNAME);
				value.setMappingName(mappingName);
				final String nodeValue = XmlPaserUtil.getNodeValue(valueItem);
				value.setValue(nodeValue);
				valueList.put(nodeValue, value);
			}
		}
		return valueList;
	}
}
